package game.players.states;

import game.ball.Possessed;
import game.ball.SoccerBall;
import game.components.MatchScene;
import game.players.SoccerPlayer;

import com.uqbar.vainilla.DeltaState;
import com.uqbar.vainilla.events.constants.Key;

public class UserWithoutBall extends WithoutBall {
	
	private int timer = 5;
	
	public UserWithoutBall(SoccerPlayer player) {
		super(player);
	}

	public void update(DeltaState deltaState) {
		if(deltaState.isKeyBeingHold(Key.LEFT) && this.getPlayer().getX() > 0)
			this.getPlayer().move(-this.getPlayer().getMovementSpeed(), 0);
		if(deltaState.isKeyBeingHold(Key.RIGHT) && this.getPlayer().getX() < this.getPlayer().getGame().getDisplayWidth() - this.getPlayer().getAppearance().getWidth())
			this.getPlayer().move(this.getPlayer().getMovementSpeed(), 0);
		if(deltaState.isKeyBeingHold(Key.UP) && this.getPlayer().getY() > 0)
			this.getPlayer().move(0, -this.getPlayer().getMovementSpeed());
		if(deltaState.isKeyBeingHold(Key.DOWN) && this.getPlayer().getY() < this.getPlayer().getGame().getDisplayHeight() - this.getPlayer().getAppearance().getHeight())
			this.getPlayer().move(0, this.getPlayer().getMovementSpeed());
		
		if(this.timer <= 0)
			this.checkBallCollides();		
		else
			this.timer--;
		
		//cambio de jugador
		if(deltaState.isKeyPressed(Key.C) && this.timer < 4){//aprovecho el timer para que no se ejecute varias veces sequidas al apretar la tecla una sola vez
			MatchScene scene = this.getPlayer().getScene();
			SoccerPlayer closerBallPlayer = scene.getCloserBallTeammate(this.getPlayer());
			closerBallPlayer.setState(new UserWithoutBall(closerBallPlayer));
			scene.setUserPlayer(closerBallPlayer);
			closerBallPlayer.addUserPlayerMark();
		}
			
	}

	@Override
	public void changeBallPlayerStates(SoccerBall ball, SoccerPlayer player) {
		player.setState(new UserWithBall(player));
		ball.setState(new Possessed(ball, player));		
	}


}
